<template>
  <div class="login-container">
    <article class="header">
      <header>
        <el-avatar icon="el-icon-user-solid" shape="circle" />
        <span class="login">
          <em class="bold">已有账号？</em>
          <el-button type="primary" size="mini" @click="gologin">去登录</el-button>
        </span>
      </header>
    </article>
    <section>
      <el-form ref="loginForm" :model="loginForm" :rules="rules" class="login-form">

        <!--        <div class="title-container">-->
        <!--          <h3 class="title">注册</h3>-->
        <!--        </div>-->
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="用户名"
            type="text"
            tabindex="1"
            @blur="reload"
          />
        </el-form-item>

        <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="密码"
              tabindex="2"
              autocomplete="on"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
        </el-tooltip>
        <el-form-item prop="realname">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="realname"
            v-model="loginForm.realname"
            placeholder="请输入姓名"
            name="realname"
            type="text"
            tabindex="3"
          />
        </el-form-item>
        <!-- 手机 -->
        <el-form-item prop="phone">
          <span class="svg-container">
            <svg-icon icon-class="phone" />
          </span>
          <el-input
            ref="phone"
            v-model="loginForm.phone"
            placeholder="请输入手机号"
            name="phone"
            type="text"
            tabindex="4"
          />
        </el-form-item>
        <!-- 邮箱 -->
        <el-form-item prop="email">
          <span class="svg-container">
            <svg-icon icon-class="email" />
          </span>
          <el-input
            ref="email"
            v-model="loginForm.email"
            placeholder="请输入邮箱"
            name="email"
            type="text"
            tabindex="5"
          />
        </el-form-item>
        <el-form-item prop="status">
          <span class="svg-container" style="margin-right: 15px">
            <svg-icon icon-class="用户状态" />
          </span>
          <el-switch
            v-model="loginForm.status"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>

        <el-form-item style="border: 1px solid rgba(250, 247, 247, 0);">
          <el-button type="primary" style="width:40%;margin-right:17.8%;" @click="register">注册</el-button>
          <el-button type="primary" style="width:40%;" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </section>
  </div>
</template>

<script>
// import { register } from '@/api/user'
// import axios from 'axios'
// import { request } from 'axios'
import request from '@/utils/request'

export default {
  data() {
    var checkEmail = (rule, value, callback) => {
      var reg = /^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*\.[a-z]{2,}$/
      if (!reg.test(value)) {
        return callback(new Error('邮箱格式错误'))
      }
      // 一定要写callback()
      callback()
    }
    return {
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }
        ],
        realname: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' }
        ]
      },
      loginForm: {

      },
      capsTooltip: false,
      passwordType: 'password',
      recapsTooltip: false,
      repasswordType: 'password',
      verifycodeImg: ''
    }
  },
  methods: {

    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    reshowPwd() {
      if (this.repasswordType === 'password') {
        this.repasswordType = ''
      } else {
        this.repasswordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.repassword.focus()
      })
    },
    recheckCapslock(e) {
      const { key } = e
      this.recapsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    reload() {
      //  "路径?t=" + new Date() ，提供一个t变量，用于唯一标识每一次访问路径
      // this.verifycodeImg = `http://localhost:10010/v2/user-service/verifycode/${this.loginForm.username}?t=` + new Date().getTime()
      this.rerifycodeImg = `${process.env.VUE_APP_BASE_API}/user-service/verifycode/${this.loginForm.username}?t=${new Date().getTime()}`
    },
    register() {
      // if (this.form.password !== this.form.confirm) {
      //   this.$message({
      //     type: 'error',
      //     message: '2次密码输入不一致！'
      //   })
      //   return
      // }

      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          request.post('/user/register', this.loginForm).then(response => {
            this.$message({
              type: 'success',
              message: '注册成功'
            })
            this.$router.push({ path: '/login' }) // 登录成功之后进行页面的跳转，跳转到主页
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    gologin() {
      this.$router.push({ path: '/login' })
    },
    // 重置
    resetLoginForm() {
      this.$refs.loginForm.resetFields()
    }

  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

// $bg:#283443;
$bg:#e6eaef;
$light_gray:#fff;
$cursor: rgb(19, 19, 19);
// $cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 25px;
      padding: 12px 5px 12px 15px;
      color: rgb(255, 252, 252);
      height: 46px;
      caret-color: rgba(62,119,194,0.8);

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  input:-moz-placeholder,
  textarea:-moz-placeholder {
    color: #000;
  }
  input:-ms-input-placeholder,
  textarea:-ms-input-placeholder {
    color: #000;
  }
  input::-webkit-input-placeholder,
  textarea::-webkit-input-placeholder {
    color: #000 ;
  }
  .el-form-item {
    border: 1px solid rgba(250, 247, 247, 0.5);
    // background: rgba(0, 0, 0, 0.1);
    //background: rgba(172,172,172,0.1);
    border-radius: 5px;
    // color: #454545;
    color: #ffffff;
  }
}
</style>

<style lang="scss" scoped>
// $bg:#2d3a4b;
// $dark_gray:#889aa4;
// $light_gray:#eee;
$bg: #7a7878;
$dark_gray: #000000;
$light_gray:#eee;
.login-container {

  width: 100%;
  height: 100%;
  //background-color: $bg;
  overflow: hidden;
  background-image: url('../../assets/欢迎.jpg');
  background-position: center center;
  /* 背景图不平铺 */
  background-repeat: no-repeat;
  /* 当内容高度大于图片高度时，背景图像的位置相对于viewport固定 */
  background-attachment: fixed;
  /* 让背景图基于容器大小伸缩 */
  background-size: cover;
  /* 设置背景颜色，背景图加载过程中会显示背景色 */
  background-color: #464646;
  .login-form {
    position: relative;
    width: 600px;
    max-width: 100%;
    padding: 50px 50px 10px;
    margin: 0 auto;
    overflow: hidden;
    background-color: rgba(199, 197, 197, 0.5);
    border-radius:5%;
    color: #000;
  }
  .header {
    border-bottom: 2px solid rgb(255, 253, 253);
    min-width: 980px;
    color: #666;

    header {
      margin: 0 auto;
      padding: 10px 0;
      width: 980px;

      .login {
        float: right;
      }

      .bold {
        font-style: normal;
        color: #000000;
      }
    }
  }

  > section {
    margin: 0 auto 30px;
    padding-bottom: 100px;
    width: 980px;
    min-height: 300px;
    box-sizing: border-box;

    .status {
      font-size: 12px;
      margin-left: 20px;
      color: #e6a23c;
    }

    .error {
      color: red;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 60px;
      color: #1e89ec;
      margin: -120px auto 0px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}

</style>

